﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Interop;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Converters;
using System.Windows.Media.Composition;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using System.Windows.Shapes;
using System.Runtime.InteropServices;

namespace ModernClockGadget
{
    public class ModernClock : GadgetBase.GadgetWindow
    {

        public ModernClock()
            : base()
        {
            this.Title = "Modern Clock";

            base.Width = 130;
            base.Height = 130;

            Clock clock = new Clock();
            base.Children.Add(clock);
        }


        public class Clock : GadgetBase.Gadgets.Gadget
        {
            private string folder;

            private RotateTransform hours = new RotateTransform();
            private RotateTransform minutes = new RotateTransform();
            private RotateTransform seconds = new RotateTransform();

            private Image h = new Image();
            private Image m = new Image();
            private Image s = new Image();
            private Image dot = new Image();

            private DropShadowBitmapEffect hourShadow;
            private DropShadowBitmapEffect minuteShadow;
            private DropShadowBitmapEffect secondShadow;

            private string themaName;
            private double imageWidth;
            private double imageHeight;
            private Thickness imageMargin;
            private DropShadowBitmapEffect shadow = new DropShadowBitmapEffect();

            private const string backgroundImagePath = "{0}{1}.png";
            private const string hoursImagePath = "{0}{1}_h.png";
            private const string minutesImagePath = "{0}{1}_m.png";
            private const string secondsImagePath = "{0}{1}_s.png";
            private const string dotImagePath = "{0}{1}_dot.png";

            public DispatcherTimer timer = new DispatcherTimer();

            public Clock()
            {
                this.shadow.ShadowDepth = 2;
                this.shadow.Opacity = 0.4;
                this.shadow.Direction = 315;
                this.shadow.Softness = 0;

                this.folder = GadgetBase.GlobalMethods.GetFullPath(@"images\");

                LoadThema("modern");

                h.Stretch = Stretch.UniformToFill;
                m.Stretch = Stretch.UniformToFill;
                s.Stretch = Stretch.UniformToFill;

                this.hourShadow = shadow;
                this.minuteShadow = shadow.Clone();
                this.secondShadow = shadow.Clone();
                h.BitmapEffect = this.hourShadow;
                m.BitmapEffect = this.minuteShadow;
                s.BitmapEffect = this.secondShadow;

                h.RenderTransformOrigin = new Point(0.5, 0.5);
                h.RenderTransform = this.hours;
                m.RenderTransformOrigin = new Point(0.5, 0.5);
                m.RenderTransform = this.minutes;
                s.RenderTransformOrigin = new Point(0.5, 0.5);
                s.RenderTransform = this.seconds;

                RefreshTime();

                base.Children.Add(h);
                base.Children.Add(m);
                base.Children.Add(s);
                base.Children.Add(dot);

                this.timer.Interval = new TimeSpan(0, 0, 0, 0, 500);
                this.timer.Tick += new EventHandler(this.timer_Tick);
                this.timer.IsEnabled = true;
            }

            private void timer_Tick(object sender, EventArgs e)
            {
                RefreshTime();
            }

            private void RefreshTime()
            {
                DateTime time = DateTime.Now;
                this.seconds.Angle = time.Second * 6;
                this.minutes.Angle = (time.Minute * 6) + (time.Second / 10);
                this.hours.Angle = ((time.Hour > 12 ? time.Hour - 12 : time.Hour) * 30) + (time.Minute * 0.5);
                FixShadowAngle();
            }

            private void FixShadowAngle()
            {
                this.hourShadow.Direction = -(45 - this.hours.Angle);
                this.minuteShadow.Direction = -(45 - this.minutes.Angle);
                this.secondShadow.Direction = -(45 - this.seconds.Angle);
            }


            private void SetImage(Image image, string path)
            {
                image.Source = GadgetBase.GlobalMethods.GetBitmapImage(path);
                image.Width = this.imageWidth;
                image.Height = this.imageHeight;
                image.Margin = this.imageMargin;
            }

            private void SetImages()
            {
                SetBackground(string.Format(backgroundImagePath, folder, this.themaName));
                SetImage(this.h, string.Format(hoursImagePath, folder, this.themaName));
                SetImage(this.m, string.Format(minutesImagePath, folder, this.themaName));
                SetImage(this.s, string.Format(secondsImagePath, folder, this.themaName));
            }

            private void LoadThema(string name)
            {
                this.themaName = name;
                this.imageWidth = 13;
                this.imageHeight = 129;
                this.imageMargin = new Thickness(58, 0, 0, 0);

                SetImages();
            }
        }

    }
}
